//+--------+ //|Shell_16 //+--------+ #property copyright "Ron Thompson" #property link "http://www.lightpatch.com/forex" // This EA is NEVER to be SOLD individually // This EA is NEVER to be INCLUDED as part of a collection that is SOLD /* If enabled (IStep greater than 1) the system will double lot sizes when there is a loss. This is an attempt to recover the previous loss, and make money on the current trade. And there are styles (IStyle) of lot growth for loss recovery The greatest failure of this EA will come when you don't have enough money to support the Martingles steps anymore, so start your lot sizes small. LotIncreasement grows the lot size based on your starting balance, risking more money when you have more money. It is built in, based on Starting Balance and LotResolution, and is independent of IStep and the Martingale process. ---Starting Balance--- The formula for Lots is AccountBalance() / StartingBalance Lets say your AccountBalance() is currently 550$ if you want to trade 10.0 lots, put 55 into StartingBalance 550 / 55 = 10 if you want to trade 1.0 lots, put 550 into StartingBalance 550 / 550 = 1 if you want to trade 0.5 lots, put 1100 into StartingBalance 550 / 1100 = 0.5 if you want to trade 0.1 lots, put 5500 into StartingBalance 550 / 5500 = 0.1 if you want to trade 0.01 lots, put 55000 into StartingBalance 550 / 55000 = 0.01 ---LotResolution--- This controls the Lot size of step your broker/account can support If set to 1, then your lot size is increased in 0.1 steps as your account grows If set to 2, then your lot size is increased in 0.01 steps as your account grows IBFX supports LotResolution of 2 ---IStep--- IStep is ONLY USED WHEN IStyle==1 IStep is the USER control for progression of lot growth to recover loss Lots are multiplied by this number for each losing trade ---IStyle--- Allows you to pick the type of loss recovery you're most comfortible with. 0 == NO LOSS RECOVERY 1 == lot growth based on IStep 2 == Modified Martingale - growth by 2.0000 with 1st step being 1 3 == Fibonacci - growth by Fibo numbers, requires 2 wins to recover the total loss 4 == future implementation ---SameBarRecovery--- Usually there is only 1 trade allowed per bar. If this option is set to true, then another trade will be allowed on the same bar, but only if the last trade was a loss. ---MinFreeMarginPct--- This percent of your account must be free for margin before an order will be placed ---ProfitMade--- The amount of pips you expect to make on EACH TRADE ---LossLimit--- The amount of pips you can afford to lose on EACH TRADE ---Other elements--- The init() section has a place to put in dates and times for disabling trades This can be quite useful if you want to stop trading during certain hours or days There can be up to 100 different begin/end time pairs Once the begin date/time arrives, no new orders will be opened until after the end date/time Trades are constrained to one-per-bar. This is controlled in the // bar counting section of the code. Trade is only enabled if there are no orders open at the new bar. Search for //uncomment for multiple trades per bar There is also an override for this using SameBarRecovery but only for losing trades */ // EA SPECIFIC extern double BDistance = 14; // plus how much extern int BPeriod = 15; // Bollinger period extern double Deviation = 1.8; // Bollinger deviation // user input extern int IStyle = 0; extern double IStep = 2; extern bool SameBarRecovery = false; extern double StartingBalance = 5000; extern double MinFreeMarginPct = 25.0; extern double ProfitMade = 8; extern double BasketProfit = 0; extern double LossLimit = 9; extern double BasketLoss = 0; extern double BreakEven = 0; extern double TrailStop = 0; extern bool LotIncrease = true ; extern int LotResolution = 1 ; extern bool KillLogging = true ; // Trade control double Lots; // size of trades, calculated in init() double lotsi; // used for Martingale loss recovery int Slippage=2; // how many pips of slippage can you tolorate bool TradeAllowed=true; // used to manage trades int OrderBar=0; // what bar was the order placed on int loopcount; // count of order attempts int maxloop=25; // maximum number of attempts to handle errors int LL2SL=10; // LossLimit to StopLoss server spread int maxOrders; // statistic for maximum numbers or orders open at one time double maxLots; // Largest lot size ever opened int MagicNumber = 142537; // allows multiple experts to trade on same account string TradeComment = "_shell16.txt"; // where to log information // Bar handling datetime bartime=0; // used to determine when a bar has moved int bartick=0; // number of times bars have moved // Time Control string SST[200]; // Start-Stop Time array used to manage time avoidance // Loss Recovery int lotptr=0; // array pointer for Loss Recovery int maxptr=9; // maximum Loss Recovery array element int Step[10]; // how to step the IStep pointer // used for verbose error logging #include //+-------------+ //| Custom init | //|-------------+ // Called ONCE when EA is added to chart or recompiled int init() { // Even numbered elements are STOP times // Odd numbered elements are START times // // THESE ARE IN SERVER-SIDE TIME!!!!!! // (usually GMT, your broker may vary) // SST[0] ="2006.11.07 13:15:00"; SST[1] ="2006.11.07 15:15:00"; if(MinFreeMarginPct==0) MinFreeMarginPct=1; if(LotResolution<1 || LotResolution>2) LotResolution=1; Lots=NormalizeDouble(AccountBalance()/StartingBalance,LotResolution); lotsi=Lots; logwrite(TradeComment,"LotIncrease ACTIVE Account balance="+AccountBalance()+" Lots="+Lots+" StartingBalance="+StartingBalance); // NO LOSS RECOVERY if (IStyle==0) { logwrite(TradeComment,"Loss Recovery Style = 0 - NONE"); Step[0]=1; Step[1]=1; Step[2]=1; Step[3]=1; Step[4]=1; Step[5]=1; Step[6]=1; Step[7]=1; Step[8]=1; Step[9]=1; } // USER steps based on IStep if (IStyle==1) { logwrite(TradeComment,"Loss Recovery Style = 1 - USER"); int stylei=1; Step[0]=stylei; logwrite(TradeComment,"Step0="+stylei); stylei=stylei*IStep; Step[1]=stylei; logwrite(TradeComment,"Step1="+stylei); stylei=stylei*IStep; Step[2]=stylei; logwrite(TradeComment,"Step2="+stylei); stylei=stylei*IStep; Step[3]=stylei; logwrite(TradeComment,"Step3="+stylei); stylei=stylei*IStep; Step[4]=stylei; logwrite(TradeComment,"Step4="+stylei); stylei=stylei*IStep; Step[5]=stylei; logwrite(TradeComment,"Step5="+stylei); stylei=stylei*IStep; Step[6]=stylei; logwrite(TradeComment,"Step6="+stylei); stylei=stylei*IStep; Step[7]=stylei; logwrite(TradeComment,"Step7="+stylei); stylei=stylei*IStep; Step[8]=stylei; logwrite(TradeComment,"Step8="+stylei); stylei=stylei*IStep; Step[9]=stylei; logwrite(TradeComment,"Step9="+stylei); stylei=stylei*IStep; } // Martingale if (IStyle==2) { // slightly modified to start at 1 since the current order // plus the new order means it's doubled logwrite(TradeComment,"Loss Recovery Style = 2 - MART"); Step[0]=1; Step[1]=1; Step[2]=2; Step[3]=4; Step[4]=8; Step[5]=16; Step[6]=32; Step[7]=64; Step[8]=128; Step[9]=256; } // Fibonacci if (IStyle==3) { logwrite(TradeComment,"Loss Recovery Style = 3 - FIBO"); Step[0]=1; Step[1]=1; Step[2]=2; Step[3]=3; Step[4]=5; Step[5]=8; Step[6]=13; Step[7]=21; Step[8]=34; Step[9]=55; } logwrite(TradeComment,"Init Complete"); Comment(" "); } //+----------------+ //| Custom DE-init | //+----------------+ // Called ONCE when EA is removed from chart int deinit() { // always indicate deinit statistics logwrite(TradeComment,"MAX number of orders "+maxOrders); logwrite(TradeComment,"Max Lots is "+maxLots); logwrite(TradeComment,"DE-Init Complete"); Comment(" "); } //+-----------+ //| Main | //+-----------+ // Called EACH TICK and each Bar[] int start() { int cnt=0; int gle=0; int ticket=0; int OrdersPerSymbol=0; // stoploss and takeprofit and close control double SL=0; double TP=0; double CurrentProfit=0; double CurrentBasket=0; // direction control bool BUYme=false; bool SELLme=false; //safety counter int loopcount=0; // bar counting if(bartime!=Time[0]) { bartime=Time[0]; bartick++; OrdersPerSymbol=0; for(cnt=OrdersTotal();cnt>=0;cnt--) { OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES); if( OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber) OrdersPerSymbol++; } // this allows only one trade per bar preventing // lots of trades on big, single bar moves if(OrdersPerSymbol==0) TradeAllowed=true; logwrite(TradeComment,"New Bar at"); } // Lot increasement based on AccountBalance if(LotIncrease) { Lots=NormalizeDouble(AccountBalance()/StartingBalance,LotResolution); if( Lots>MarketInfo(Symbol(), MODE_MAXLOT) ) Lots=MarketInfo(Symbol(), MODE_MAXLOT); } OrdersPerSymbol=0; for(cnt=OrdersTotal();cnt>=0;cnt--) { OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES); if( OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber) { OrdersPerSymbol++; } } //uncomment for multiple trades per bar // //if(OrdersPerSymbol==0) // { // TradeAllowed=true; // } // keep some statistics if(OrdersPerSymbol>maxOrders) maxOrders=OrdersPerSymbol; //+-----------------------------+ //| Insert your indicator here | //| And set either BUYme or | //| SELLme true to place orders | //+-----------------------------+ double ma = iMA(Symbol(),0,BPeriod,0,MODE_SMA,PRICE_OPEN,0); double stddev = iStdDev(Symbol(),0,BPeriod,0,MODE_SMA,PRICE_OPEN,0); double bup = ma+(Deviation*stddev); double bdn = ma-(Deviation*stddev); if(Close[0]>bup+(BDistance*Point)) SELLme=true; if(Close[0]=gstart && Time[0]<=gstop) { Print( Time[0],CurTime() ); TradesOff=true; } } //ENTRY LONG (buy, Ask) if( TradeAllowed && BUYme && !TradesOff) { OpenBuy(); } //ENTRY SHORT (sell, Bid) if( TradeAllowed && SELLme && !TradesOff) { OpenSell(); } //Basket profit or loss CurrentBasket=AccountEquity()-AccountBalance(); if( BasketProfit>0 && CurrentBasket>=BasketProfit ) CloseEverything(); if( BasketLoss >0 && CurrentBasket<=(BasketLoss*(-1)) ) CloseEverything(); // // Order Management // for(cnt=OrdersTotal();cnt>=0;cnt--) { OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES); if( OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber ) { if(OrderType()==OP_BUY) { CurrentProfit=(Bid-OrderOpenPrice()) ; // // Modify for break even //======================= // // OrderStopLoss will be equal to OrderOpenPrice if this event happens // thus it will only ever get executed one time per ticket if( BreakEven>0 ) { if (CurrentProfit >= BreakEven*Point && OrderOpenPrice()>OrderStopLoss()) { SL=OrderOpenPrice()+(Ask-Bid); TP=OrderTakeProfit(); OrderModify(OrderTicket(),OrderOpenPrice(),SL,TP, White); gle=GetLastError(); if(gle==0) { logwrite(TradeComment,"MODIFY BUY BE Ticket="+OrderTicket()+" SL="+SL+" TP="+TP); } else { logwrite(TradeComment,"-----ERROR----- MODIFY BUY BE Bid="+Bid+" error="+gle+" "+ErrorDescription(gle)); } } } // // check for trailing stop //========================= // if( OrderProfit()>0 && TrailStop>0 ) { if( OrderStopLoss() < Bid-(TrailStop*Point) ) { SL=Bid-(TrailStop*Point); TP=OrderTakeProfit(); OrderModify(OrderTicket(),OrderOpenPrice(),SL,TP,0,White); gle=GetLastError(); if(gle==0) { logwrite(TradeComment,"MODIFY BUY TS Ticket="+OrderTicket()+" SL="+SL+" TP="+TP); } else { logwrite(TradeComment,"-----ERROR----- MODIFY BUY TS Bid="+Bid+" error="+gle+" "+ErrorDescription(gle)+" "); } } } // Did we make a profit //====================== if(ProfitMade>0 && CurrentProfit>=(ProfitMade*Point)) { CloseBuy("PROFIT"); lotsi=Lots; lotptr=0; } // Did we take a loss //==================== if(LossLimit>0 && CurrentProfit<=(LossLimit*(-1)*Point)) { CloseBuy("LOSS"); lotptr++; if(lotptr>maxptr) lotptr=0; logwrite(TradeComment,"BUY lotptr="+lotptr+" Step="+Step[lotptr]); lotsi=NormalizeDouble(lotsi*Step[lotptr],LotResolution); logwrite(TradeComment,"BUY lotsi="+lotsi); if(SameBarRecovery && OrderBar==bartick) { logwrite(TradeComment,"BUY LOSS Allowing another trade during same bar"); TradeAllowed=true; } } } // if BUY if(OrderType()==OP_SELL) { CurrentProfit=(OrderOpenPrice()-Ask); //logwrite(TradeComment,"SELL CurrentProfit="+CurrentProfit/Point+" CurrentBasket="+CurrentBasket/Point); // // Modify for break even //======================= // // OrderStopLoss will be equal to OrderOpenPrice if this event happens // thus it will only ever get executed one time per ticket if( BreakEven>0 ) { if (CurrentProfit >= BreakEven*Point && OrderOpenPrice()0 && TrailStop>0) { if( OrderStopLoss() > Ask+(TrailStop*Point) ) { SL=Ask+(TrailStop*Point); TP=OrderTakeProfit(); OrderModify(OrderTicket(),OrderOpenPrice(),SL,TP,0,Red); gle=GetLastError(); if(gle==0) { logwrite(TradeComment,"MODIFY SELL TS Ticket="+OrderTicket()+" SL="+SL+" TP="+TP); } else { logwrite(TradeComment,"-----ERROR----- MODIFY SELL TS Ask="+Ask+" error="+gle+" "+ErrorDescription(gle)); } } } // Did we make a profit //====================== if( ProfitMade>0 && CurrentProfit>=(ProfitMade*Point) ) { CloseSell("PROFIT"); lotsi=Lots; lotptr=0; } // Did we take a loss //==================== if( LossLimit>0 && CurrentProfit<=(LossLimit*(-1)*Point) ) { CloseSell("LOSS"); lotptr++; if(lotptr>maxptr) lotptr=0; logwrite(TradeComment,"SELL lotptr="+lotptr+" Step="+Step[lotptr]); lotsi=NormalizeDouble(lotsi*Step[lotptr],LotResolution); logwrite(TradeComment,"SELL lotsi="+lotsi); if(SameBarRecovery && OrderBar==bartick) { logwrite(TradeComment,"BUY LOSS Allowing another trade during same bar"); TradeAllowed=true; } } } //if SELL } // if(OrderSymbol) } // for } // start() //+-----------------+ //| CloseEverything | //+-----------------+ // Closes all OPEN and PENDING orders int CloseEverything() { int i; for(i=OrdersTotal();i>=0;i--) { OrderSelect(i, SELECT_BY_POS); if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber) { if(OrderType()==OP_BUY) CloseBuy ("BASKET"); if(OrderType()==OP_SELL) CloseSell("BASKET"); if(OrderType()==OP_BUYLIMIT) OrderDelete( OrderTicket() ); if(OrderType()==OP_SELLLIMIT) OrderDelete( OrderTicket() ); if(OrderType()==OP_BUYSTOP) OrderDelete( OrderTicket() ); if(OrderType()==OP_SELLSTOP) OrderDelete( OrderTicket() ); } Sleep(1000); } //for } // closeeverything // log data to a file name passed in // print everything regardless of log setting void logwrite (string filename, string mydata) { int myhandle; string gregorian=TimeToStr(CurTime(),TIME_DATE|TIME_SECONDS); Print(mydata+" "+gregorian); // don't log anything if testing or if user doesn't want it if(IsTesting()) return(0); if(KillLogging) return(0); myhandle=FileOpen(Symbol()+"_"+filename, FILE_CSV|FILE_WRITE|FILE_READ, ";"); if(myhandle>0) { FileSeek(myhandle,0,SEEK_END); FileWrite(myhandle, mydata+" "+gregorian); FileClose(myhandle); } } //ENTRY LONG (buy, Ask) void OpenBuy() { int gle=0; int ticket=0; double SL=0; double TP=0; int loopcount=0; while(true) { if( AccountFreeMargin()< (AccountBalance()*(MinFreeMarginPct/100)) ) { logwrite(TradeComment,"Your BUY equity is too low to trade"); break; } if(LossLimit ==0) SL=0; else SL=Ask-((LossLimit+LL2SL)*Point ); if(ProfitMade==0) TP=0; else TP=Ask+((ProfitMade+LL2SL)*Point ); ticket=OrderSend(Symbol(),OP_BUY,lotsi,Ask,Slippage,SL,TP,TradeComment,MagicNumber,White); gle=GetLastError(); if(gle==0) { logwrite(TradeComment,"BUY Ticket="+ticket+" Ask="+Ask+" Lots="+lotsi+" SL="+SL+" TP="+TP); TradeAllowed=false; OrderBar=bartick; break; } else { logwrite(TradeComment,"-----ERROR----- opening BUY order: Lots="+lotsi+" SL="+SL+" TP="+TP+" Bid="+Bid+" Ask="+Ask+" ticket="+ticket+" Err="+gle+" "+ErrorDescription(gle)); RefreshRates(); Sleep(500); loopcount++; if(loopcount>maxloop) break; } }//while }//BUYme //ENTRY SHORT (sell, Bid) void OpenSell() { int gle=0; int ticket=0; double SL=0; double TP=0; int loopcount=0; while(true) { if( AccountFreeMargin()< (AccountBalance()*(MinFreeMarginPct/100)) ) { logwrite(TradeComment,"Your SELL equity is too low to trade"); break; } if(LossLimit ==0) SL=0; else SL=Bid+((LossLimit+LL2SL)*Point ); if(ProfitMade==0) TP=0; else TP=Bid-((ProfitMade+LL2SL)*Point ); ticket=OrderSend(Symbol(),OP_SELL,lotsi,Bid,Slippage,SL,TP,TradeComment,MagicNumber,Red); gle=GetLastError(); if(gle==0) { logwrite(TradeComment,"SELL Ticket="+ticket+" Bid="+Bid+" Lots="+lotsi+" SL="+SL+" TP="+TP); TradeAllowed=false; OrderBar=bartick; break; } else { logwrite(TradeComment,"-----ERROR----- opening SELL order: Lots="+lotsi+" SL="+SL+" TP="+TP+" Bid="+Bid+" Ask="+Ask+" ticket="+ticket+" Err="+gle+" "+ErrorDescription(gle)); RefreshRates(); Sleep(500); loopcount++; if(loopcount>maxloop) break; } }//while }//SELLme void CloseBuy (string myInfo) { int gle; int cnt; int OrdersPerSymbol; int loopcount=0; string bTK=" Ticket="+OrderTicket(); string bSL=" SL="+OrderStopLoss(); string bTP=" TP="+OrderTakeProfit(); string bPM; string bLL; string bER; bPM=" PM="+ProfitMade; bLL=" LL="+LossLimit; while(true) { OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,White); gle=GetLastError(); bER=" error="+gle+" "+ErrorDescription(gle); if(gle==0) { logwrite(TradeComment,"CLOSE BUY "+myInfo+ bTK + bSL + bTP + bPM + bLL); break; } else { logwrite(TradeComment,"-----ERROR----- CLOSE BUY "+myInfo+ bER +" Bid="+Bid+ bTK + bSL + bTP + bPM + bLL); RefreshRates(); Sleep(500); } // sometimes an order close is delayed, or a gap jumps to the LL2SL on the server // This keeps a server-closed order from hanging here OrdersPerSymbol=0; for(cnt=OrdersTotal();cnt>=0;cnt--) { OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES); if( OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber) OrdersPerSymbol++; } if(OrdersPerSymbol==0) break; loopcount++; if(loopcount>maxloop) break; }//while } void CloseSell (string myInfo) { int gle; int cnt; int OrdersPerSymbol; int loopcount=0; string sTK=" Ticket="+OrderTicket(); string sSL=" SL="+OrderStopLoss(); string sTP=" TP="+OrderTakeProfit(); string sPM; string sLL; string sER; sPM=" PM="+ProfitMade; sLL=" LL="+LossLimit; while(true) { OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,Red); gle=GetLastError(); sER=" error="+gle+" "+ErrorDescription(gle); if(gle==0) { logwrite(TradeComment,"CLOSE SELL "+myInfo + sTK + sSL + sTP + sPM + sLL); break; } else { logwrite(TradeComment,"-----ERROR----- CLOSE SELL "+myInfo+ sER +" Ask="+Ask+ sTK + sSL + sTP + sPM + sLL); RefreshRates(); Sleep(500); } // sometimes an order close is delayed, or a gap jumps to the LL2SL on the server // This keeps a server-closed order from hanging here OrdersPerSymbol=0; for(cnt=OrdersTotal();cnt>=0;cnt--) { OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES); if( OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber) OrdersPerSymbol++; } if(OrdersPerSymbol==0) break; loopcount++; if(loopcount>maxloop) break; }//while } /* double LSMA(int myLPeriod, int myShift ) { int myI; double myLengthvar; double myTmp; double mySum=0; for(myI = myLPeriod; myI >= 1 ; myI--) { myLengthvar = myLPeriod + 1; myLengthvar /= 3; myTmp = 0; myTmp = ( myI - myLengthvar)*Open[myLPeriod-myI+myShift]; mySum+=myTmp; } return( mySum*6/(myLPeriod*(myLPeriod+1)) ); } */